using System.Collections.Generic;
using UnityEngine;

public class AudioController : MonoInstance<AudioController>
{
	private const int WILL_PAUSE_AUDIO_COUNT = 3;

	public AudioSource menubg;

	public AudioSource gamebg;

	public AudioSource FighterEngine;

	public AudioSource uibtn;

	public AudioSource uibtn1;

	public AudioSource uipophit;

	public AudioSource uibuy;

	public AudioSource uigetmoney;

	public AudioSource uigetdiamond;

	public AudioSource uiequiped;

	public AudioSource gunshot;

	public AudioSource missileshot;

	public AudioSource[] bullethit;

	public AudioSource missilehit;

	public AudioSource uigameover_suc;

	public AudioSource uigameover_fail;

	public AudioSource[] uistar;

	private int curstar;

	public AudioSource playerexp;

	public AudioSource planeexp;

	public AudioSource shipexp;

	public AudioSource buildexp;

	public AudioSource warningsou;

	public AudioSource boxsou;

	public List<AudioSource> currentLoopAudioList_Music = new List<AudioSource>();

	public List<AudioSource> currentLoopAudioList_Sound = new List<AudioSource>();

	public AudioSource[] willPauseAudioList;

	public bool[] isAudioPlayingList;

	private int randomhit;

	private bool iswarn;

	private new void Awake()
	{
		base.Awake();
		initAudioList();
	}

	public void reset()
	{
		currentLoopAudioList_Music.Clear();
		currentLoopAudioList_Sound.Clear();
	}

	private void initAudioList()
	{
		willPauseAudioList = new AudioSource[3];
		isAudioPlayingList = new bool[3];
		willPauseAudioList[0] = gamebg;
		willPauseAudioList[1] = menubg;
		willPauseAudioList[2] = FighterEngine;
		for (int i = 0; i < isAudioPlayingList.Length; i++)
		{
			isAudioPlayingList[i] = false;
		}
	}

	public void play(AudioType audiotp, float dis = 0f)
	{
		bool flag = false;
		AudioSource audioSource = null;
		bool flag2 = false;
		switch (audiotp)
		{
		case AudioType.Menu_BG:
			if (!menubg.isPlaying)
			{
				flag = true;
				audioSource = menubg;
				flag2 = true;
			}
			break;
		case AudioType.Game_BG:
			if (!gamebg.isPlaying)
			{
				flag = true;
				audioSource = gamebg;
				flag2 = true;
			}
			break;
		case AudioType.Engine:
			if (!FighterEngine.isPlaying)
			{
				flag = true;
				audioSource = FighterEngine;
				flag2 = true;
			}
			break;
		case AudioType.UI_BTN:
			if (!uibtn.isPlaying)
			{
				flag = true;
				audioSource = uibtn;
			}
			break;
		case AudioType.UI_BTN1:
			if (!uibtn1.isPlaying)
			{
				flag = true;
				audioSource = uibtn1;
			}
			break;
		case AudioType.UI_pophit:
			if (!uipophit.isPlaying)
			{
				flag = true;
				audioSource = uipophit;
			}
			break;
		case AudioType.UI_BUY:
			flag = true;
			audioSource = uibuy;
			break;
		case AudioType.UI_getmoney:
			if (!uigetmoney.isPlaying)
			{
				flag = true;
				audioSource = uigetmoney;
			}
			break;
		case AudioType.UI_getdiamond:
			if (!uigetdiamond.isPlaying)
			{
				flag = true;
				audioSource = uigetdiamond;
			}
			break;
		case AudioType.UI_equiped:
			if (!uiequiped.isPlaying)
			{
				flag = true;
				audioSource = uiequiped;
			}
			break;
		case AudioType.Gun:
			if (!gunshot.isPlaying)
			{
				flag = true;
				audioSource = gunshot;
			}
			break;
		case AudioType.Missile:
			if (missileshot.isPlaying)
			{
				missileshot.Stop();
			}
			flag = true;
			audioSource = missileshot;
			break;
		case AudioType.BulletHit:
			randomhit = Random.Range(0, 3);
			dis = 1320f;
			if (!bullethit[randomhit].isPlaying)
			{
				flag = true;
				audioSource = bullethit[randomhit];
			}
			break;
		case AudioType.MissileHit:
			if (!missilehit.isPlaying)
			{
				flag = true;
				audioSource = missilehit;
			}
			break;
		case AudioType.UI_gameover_suc:
			if (!uigameover_suc.isPlaying)
			{
				flag = true;
				audioSource = uigameover_suc;
			}
			break;
		case AudioType.UI_gameover_fail:
			if (!uigameover_fail.isPlaying)
			{
				flag = true;
				audioSource = uigameover_fail;
			}
			break;
		case AudioType.UI_star:
			flag = true;
			audioSource = uistar[curstar];
			curstar++;
			curstar %= 3;
			break;
		case AudioType.playerExplosion:
			if (!playerexp.isPlaying)
			{
				flag = true;
				audioSource = playerexp;
			}
			break;
		case AudioType.planeExplosion:
			if (!planeexp.isPlaying && !GlobalInf.gameover)
			{
				flag = true;
				audioSource = planeexp;
			}
			break;
		case AudioType.ShipExplosion:
			if (!shipexp.isPlaying)
			{
				flag = true;
				audioSource = shipexp;
			}
			break;
		case AudioType.BuildExplosion:
			if (!buildexp.isPlaying)
			{
				flag = true;
				audioSource = buildexp;
			}
			break;
		case AudioType.warning:
			if (!warningsou.isPlaying)
			{
				flag = true;
				audioSource = warningsou;
			}
			break;
		case AudioType.openbox:
			if (!boxsou.isPlaying)
			{
				flag = true;
				audioSource = boxsou;
			}
			break;
		}
		if (!flag || !(audioSource != null))
		{
			return;
		}
		if (audioSource.loop)
		{
			addToLooplist(audioSource, flag2);
		}
		if (flag2)
		{
			if (GlobalInf.ismusic)
			{
				audioSource.Play();
			}
		}
		else if (GlobalInf.issfx)
		{
			audioSource.volume = getaudio_volume(dis);
			if (!(audioSource.volume < 0.01f))
			{
				audioSource.Play();
			}
		}
	}

	public float getaudio_volume(float dis)
	{
		if (dis > 1800f)
		{
			return 0f;
		}
		if (dis < 1000f)
		{
			return 1f;
		}
		return 1f - (dis - 1000f) / 800f * 0.5f;
	}

	public void stop(AudioType soundType)
	{
		AudioSource audioSource = null;
		bool isMusic = false;
		switch (soundType)
		{
		case AudioType.Menu_BG:
			menubg.Stop();
			audioSource = menubg;
			isMusic = true;
			break;
		case AudioType.Game_BG:
			gamebg.Stop();
			audioSource = gamebg;
			isMusic = true;
			break;
		case AudioType.Engine:
			if (FighterEngine.isPlaying)
			{
				FighterEngine.Stop();
				audioSource = FighterEngine;
				isMusic = true;
			}
			break;
		case AudioType.Gun:
			if (gunshot.isPlaying)
			{
				gunshot.Stop();
				audioSource = gunshot;
			}
			break;
		case AudioType.warning:
			if (warningsou.isPlaying)
			{
				warningsou.Stop();
				audioSource = warningsou;
			}
			break;
		}
		if (audioSource != null)
		{
			removeFromLoopList(audioSource, isMusic);
		}
	}

	public void stopAll()
	{
		if (gamebg != null)
		{
			gamebg.Stop();
		}
		currentLoopAudioList_Music.Clear();
		currentLoopAudioList_Sound.Clear();
	}

	public void stopgamemusic()
	{
		if (FighterEngine.isPlaying)
		{
			FighterEngine.Stop();
		}
		if (gunshot.isPlaying)
		{
			gunshot.Stop();
		}
		if (warningsou.isPlaying)
		{
			warningsou.Stop();
		}
	}

	public void updateMusicVolume(float volum)
	{
		for (int i = 0; i < currentLoopAudioList_Music.Count; i++)
		{
			currentLoopAudioList_Music[i].volume = volum;
		}
	}

	public void updateSoundVolume(float volum)
	{
		for (int i = 0; i < currentLoopAudioList_Sound.Count; i++)
		{
			currentLoopAudioList_Sound[i].volume = volum;
		}
	}

	public void turnOffMusic()
	{
		for (int i = 0; i < currentLoopAudioList_Music.Count; i++)
		{
			currentLoopAudioList_Music[i].Stop();
		}
	}

	public void turnOnMusic()
	{
		for (int i = 0; i < currentLoopAudioList_Music.Count; i++)
		{
			currentLoopAudioList_Music[i].Play();
		}
	}

	public void turnOffSound()
	{
		for (int i = 0; i < currentLoopAudioList_Sound.Count; i++)
		{
			if (!isAudioPause(currentLoopAudioList_Sound[i]))
			{
				currentLoopAudioList_Sound[i].Stop();
			}
		}
	}

	public void turnOnSound()
	{
		for (int i = 0; i < currentLoopAudioList_Sound.Count; i++)
		{
			if (!isAudioPause(currentLoopAudioList_Sound[i]))
			{
				currentLoopAudioList_Sound[i].Play();
			}
		}
	}

	public void pauseSounds()
	{
		iswarn = false;
		for (int i = 0; i < 3; i++)
		{
			isAudioPlayingList[i] = false;
			if (willPauseAudioList[i] != null)
			{
				isAudioPlayingList[i] = true;
			}
		}
		for (int j = 0; j < 3; j++)
		{
			if (isAudioPlayingList[j])
			{
				willPauseAudioList[j].Pause();
			}
		}
		if (warningsou.isPlaying)
		{
			iswarn = true;
			warningsou.Pause();
		}
		if (gunshot.isPlaying)
		{
			gunshot.Pause();
		}
	}

	public void resumeSounds()
	{
		if (!GlobalInf.ismusic)
		{
			for (int i = 0; i < 3; i++)
			{
				if (isAudioPlayingList[i] && !willPauseAudioList[i].loop)
				{
					willPauseAudioList[i].Stop();
				}
			}
			return;
		}
		for (int j = 0; j < 3; j++)
		{
			if (isAudioPlayingList[j])
			{
				willPauseAudioList[j].Play();
			}
		}
		if (iswarn && GlobalInf.issfx)
		{
			warningsou.Play();
		}
	}

	private bool isAudioPause(AudioSource audio)
	{
		for (int i = 0; i < 3; i++)
		{
			if (isAudioPlayingList[i] == (bool)audio)
			{
				if (isAudioPlayingList[i])
				{
					return true;
				}
				return false;
			}
		}
		return false;
	}

	private void addToLooplist(AudioSource audio, bool isMusic)
	{
		if (isMusic)
		{
			if (!currentLoopAudioList_Music.Contains(audio))
			{
				currentLoopAudioList_Music.Add(audio);
			}
		}
		else if (!currentLoopAudioList_Sound.Contains(audio))
		{
			currentLoopAudioList_Sound.Add(audio);
		}
	}

	private void removeFromLoopList(AudioSource audio, bool isMusic)
	{
		if (isMusic)
		{
			currentLoopAudioList_Music.Remove(audio);
		}
		else
		{
			currentLoopAudioList_Sound.Remove(audio);
		}
	}

	public void setpitch(float ratio)
	{
		if (FighterEngine.isPlaying)
		{
			FighterEngine.pitch = Mathf.Lerp(FighterEngine.pitch, ratio + 0.1f, Time.deltaTime * 1f);
		}
	}

	public void setpitch_acc()
	{
		if (FighterEngine.isPlaying)
		{
			FighterEngine.pitch = Mathf.Lerp(FighterEngine.pitch, 1.6f, Time.deltaTime * 1f);
		}
	}

	public void setpitch_dec()
	{
		if (FighterEngine.isPlaying)
		{
			FighterEngine.pitch = Mathf.Lerp(FighterEngine.pitch, 1.1f, Time.deltaTime * 1f);
		}
	}
}
